package com.tg.base.concurrent.lock.reentrantLock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * lock、tryLock、tryLock(timeout)区别
 * Created by MagicBook on 2020/1/29.
 */
public class ReentrantLockTryTest {

    //默认非公平，false
    static Lock lock = new ReentrantLock(false);

    public static void main(String[] args) throws InterruptedException {
//        lockExample();

//        tryLockExample();

        tryLockExampleTimeOut();
    }

    /**
     * 每条线程加锁后挂起200ms
     * 线程都阻塞等待，直至获得锁
     */
    public static void lockExample() {
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + "尝试获得锁");
                lock.lock();
                System.out.println(Thread.currentThread().getName() + "成功");
                threadSleep(200);

                lock.unlock();
                System.out.println(Thread.currentThread().getName() + "释放");
            }).start();
        }
    }

    /**
     * 每条线程加锁后挂起200ms
     * 除第一条线程以外其他线程均失败
     */
    public static void tryLockExample() {
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {

                System.out.println(Thread.currentThread().getName() + "尝试获得锁");
                boolean ok = lock.tryLock();
                if (ok) {
                    System.out.println(Thread.currentThread().getName() + "成功");
                    threadSleep(200);
                    lock.unlock();
                    System.out.println(Thread.currentThread().getName() + "释放");
                } else {
                    System.out.println(Thread.currentThread().getName() + "失败");
                }

            }).start();
        }
    }

    /**
     * tryLock阻塞超时返回结果false，未超时则阻塞等待
     */
    public static void tryLockExampleTimeOut() {
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + "尝试获得锁");
                boolean ok = false;
                try {
                    ok = lock.tryLock(200L, TimeUnit.MILLISECONDS);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
                if (ok) {
                    System.out.println(Thread.currentThread().getName() + "成功");
                    //
                    threadSleep(300);
                    lock.unlock();
                    System.out.println(Thread.currentThread().getName() + "释放");
                } else {
                    System.out.println(Thread.currentThread().getName() + "失败");
                }

            }).start();
        }
    }

    private static void threadSleep(long ms) {
        try {
            TimeUnit.MILLISECONDS.sleep(ms);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}
